package com.gzcsqic.excelapp.service;

import com.gzcsqic.excelapp.common.Constants;
import com.gzcsqic.excelapp.utils.ExcelUtil;
import com.gzcsqic.excelapp.utils.OsUtil;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Map;

@Service
public class ParamService {


    public void addSi(Map<String,String> data){
        add(data,OsUtil.getPath() + Constants.SI_PARAM_FILE,false);
    }

    public void editSi(Map<String,String> data){
        edit(data,OsUtil.getPath() + Constants.SI_PARAM_FILE,false);
    }

    public void delSi(Map<String,String> data){
        del(data.get("id"),OsUtil.getPath() + Constants.SI_PARAM_FILE);
    }


    public void addGr(Map<String,String> data){
        add(data,OsUtil.getPath() + Constants.GR_PARAM_FILE,false);
    }

    public void editGr(Map<String,String> data){
        edit(data,OsUtil.getPath() + Constants.GR_PARAM_FILE,false);
    }

    public void delGr(Map<String,String> data){
        del(data.get("id"),OsUtil.getPath() + Constants.GR_PARAM_FILE);
    }


    public void addTest(Map<String,String> data){
        add(data,OsUtil.getPath() + Constants.TEST_PARAM_FILE,true);
    }

    public void editTest(Map<String,String> data){
        edit(data,OsUtil.getPath() + Constants.TEST_PARAM_FILE,true);
    }

    public void delTest(Map<String,String> data){
        del(data.get("id"),OsUtil.getPath() + Constants.TEST_PARAM_FILE);
    }


    public void addEmp(Map<String,String> data){
        add(data,OsUtil.getPath() + Constants.EMP_PARAM_FILE,false);
    }

    public void editEmp(Map<String,String> data){
        edit(data,OsUtil.getPath() + Constants.EMP_PARAM_FILE,false);
    }

    public void delEmp(Map<String,String> data){
        del(data.get("id"),OsUtil.getPath() + Constants.EMP_PARAM_FILE);
    }


    private void add(Map<String,String> data,String filePath,boolean isTestParam){
        File file = new File(filePath);
        Workbook workbook = ExcelUtil.readExcelFromFile(file);
        Sheet sheet = workbook.getSheetAt(0);
        int rowNum = sheet.getLastRowNum();

        Row row = sheet.createRow(rowNum + 1);
        for (String key : data.keySet()) {
            if(!key.equals("id")){
                int i = Integer.valueOf(key.substring(1));
                Cell cell = row.createCell(i);
                if(isTestParam && (i == 1 || i == 2 || i == 4)){
                    CellStyle style = cell.getCellStyle();
                    style.setWrapText(true); // 换行符生效
                }
                cell.setCellValue(data.get(key));
            }
        }
        ExcelUtil.saveExcel(workbook,file);
    }

    private void edit(Map<String,String> data,String filePath,boolean isTestParam){
        File file = new File(filePath);
        Workbook workbook = ExcelUtil.readExcelFromFile(file);
        Sheet sheet = workbook.getSheetAt(0);
        String id = data.get("id");
        int idx = Integer.valueOf(id);
        Row row = sheet.getRow(idx);
        for (String key : data.keySet()) {
            if(!key.equals("id")){
                int i = Integer.valueOf(key.substring(1));
                Cell cell = row.createCell(i);
                if(isTestParam && (i == 1 || i == 2 || i == 4)){
                    CellStyle style = cell.getCellStyle();
                    style.setWrapText(true); // 换行符生效
                }
                cell.setCellValue(data.get(key));
            }
        }
        ExcelUtil.saveExcel(workbook,file);
    }

    private void del(String id,String filePath){
        File file = new File(filePath);
        Workbook workbook = ExcelUtil.readExcelFromFile(file);
        Sheet sheet = workbook.getSheetAt(0);
        int idx = Integer.valueOf(id);
        int rowNum = sheet.getLastRowNum();
        if(idx == rowNum){
            rowNum++;
        }
        sheet.shiftRows(idx+1,rowNum,-1);
        ExcelUtil.saveExcel(workbook,file);
    }
}
